package com.whut.controller;

import com.alibaba.fastjson.JSONObject;
import com.whut.service.IntelligentMatchingService;
import com.whut.utils.MyDuplicatedException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 只能匹配的controller
 *
 * @author fuzhenjie
 * @date 19.11.19 10:08(GMT)
 */
@Controller
@RequestMapping("/intelMatch")
public class IntelligentMatchingController {

    private final static String SUCCESS = "success";
    private final static String ERROR = "error";

    @Resource
    private IntelligentMatchingService intelligentMatchingService;

    /**
     * 简单的匹配
     *
     * @param toMatchStr
     * @return java.lang.Object
     * @author fuzhenjie
     * @date 19.11.19 10:09(GMT)
     */
    @ResponseBody
    @PostMapping("/simpleMatch")
    public Object simpleMatch(String toMatchStr, int firstId, int secondId) {
        return intelligentMatchingService.simpleMatch(toMatchStr, firstId, secondId);
    }


    /**
     * 适用于选择之后获取匹配信息
     *
     * @param pdMkey
     * @return java.lang.Object
     * @author fuzhenjie
     * @date 19.12.6 10:59(GMT)
     */
    @ResponseBody
    @PostMapping("/getSimpleMatchInfo")
    public Object getSimpleMatchInfo(int pdMkey) {
        return intelligentMatchingService.getSimpleMatchInfo(pdMkey);
    }


    /**
     * 分页查找已有的映射信息
     *
     * @param curPage
     * @param pageSize
     * @return java.lang.Object
     * @author fuzhenjie
     * @date 19.11.19 10:26(GMT)
     */
    @ResponseBody
    @PostMapping("/getKeywordMapList")
    public Object getKeywordMapList(int curPage, int pageSize) {
        return intelligentMatchingService.getKeywordMapList(curPage, pageSize);
    }

    /**
     * 新增映射
     *
     * @param keywords list的json串
     * @param pdMkey
     * @param allowDup 是否允许关键词的包含
     * @return java.lang.Object
     * @author fuzhenjie
     * @date 19.11.19 10:27(GMT)
     */
    @ResponseBody
    @PostMapping("/addMap")
    public Object addMap(String keywords, int pdMkey, boolean allowDup) {
        JSONObject jsonObject = new JSONObject();
        try {
            intelligentMatchingService.addMap(keywords, pdMkey, allowDup);
            jsonObject.put("operationCode", SUCCESS);
        } catch (MyDuplicatedException e) {
            jsonObject.put("operationCode", ERROR);
            String message = e.getMessage();
            jsonObject.put("message", message);
        }
        return jsonObject;
    }

    /**
     * 删除映射
     *
     * @param mKey 映射表的主键
     * @return java.lang.Object
     * @author fuzhenjie
     * @date 19.11.19 10:27(GMT)
     */
    @ResponseBody
    @PostMapping("/delMap")
    public Object delMap(int mKey) {
        return intelligentMatchingService.delMap(mKey);
    }

    /**
     * 根据体系id搜索关键词映射
     *
     * @return java.lang.Object
     * @author fanyu
     * @date 2019/12/27 21:45
     * @params [system,curPage,pageSize]
     */
    @ResponseBody
    @PostMapping("/getVoiceMapInfoBySystemId")
    public Object getVoiceMapInfoBySystemId(String system,int curPage,int pageSize) {
        return intelligentMatchingService.getVoiceMapInfoBySystemId(system,curPage,pageSize);
    }
}
